 /**
 * @author caif
 * @fileoverview login module
 */
	
;(function($){
	var register = {
		onCorrect: function(el){
			$('#error-msg').css('visibility', 'hidden');
			el.closest(".control-group").removeClass("error");
			el.next().css('visibility', 'visible');
		},
		
		updateMsg : function(msg, el){
			$('#error-msg').text(msg).css('visibility', 'visible');
			if(el){
				el.closest(".control-group").addClass("error");
				el.next().css('visibility', 'hidden');
			}
		},
		
		init : function(){
			var self = this;
			
			$("#glodon_account_name").blur(function(){
		    	var t = $(this);
		    	t.val($.trim(t.val()));
		    	if(t.val() === '') return;
	    		if(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(t.val()) || /^\d{11}$/.test(t.val() )){
				    $.post("/account/check.json", {loginName: t.val()}, function(data){
			            if(data.result){
				            self.onCorrect(t);
			            }else{
			                self.updateMsg('你输入的账号已存在', t);
			            }
			        }, "json");
		        }else{
		        	self.updateMsg('请输入正确的账号', t);
		        }
				
			});
			
			var captcha = $('#captcha').append($('<img>', {src: 'captcha?t='+ new Date().getTime()})),
			captchaImg = captcha.find('img').click(function(e){
				refreshCaptcha();
			}),
			refreshCaptcha = function(){
				captchaImg.attr('src', captchaImg.attr('src') + '&t=' + new Date().getTime());
				$("#inp-captcha").select();
			};
			
			$("#inp-captcha").blur(function(){
				var t = $(this);
			    if(t.val() === ''){
		    		self.updateMsg('请输入验证码', t);
				}else{
				    $.post("captcha/verify.json",{ captchaValue : t.val()} , function(data){
			            if(data.result === 'true'){
				            self.onCorrect(t);
			            }else if(data.result === 'fail'){
			            	refreshCaptcha();
			            }else if(data.result === 'false'){
			                self.updateMsg('验证码输入错误', t);
			            }
			        }, "json");
				}
			});
			
			$("#paas_login_change").click(function(){
			    refreshCaptcha();
			    $("#paas_login_change").next("span").html("");
			});
			
			function charMode(iN) {
                if (iN >= 48 && iN <= 57) //数字    
                    return 1;
                if (iN >= 65 && iN <= 90) //大写字母    
                    return 2;
                if (iN >= 97 && iN <= 122) //小写字母    
                    return 4;
                else return 8; //特殊字符    
            }

            function bitTotal(num) {
                var modes = 0;
                for (i = 0; i < 4; i++) {
                    if (num & 1) modes++;
                    num >>>= 1;
                }
                return modes;
            }

			$("#glodon_password").blur(function(){
				var t = $(this), 
				v = t.val();
		    	if(v === ''){
		    		self.updateMsg('请输入密码', t);
				}else if(v.length < 6 || v.length > 16){
					self.updateMsg('请输入正确的密码', t);
				}else{
		    		self.onCorrect(t);
				}
			}).keyup(function(){
				var passwordComplex = 0;
                var value = $(this).val();
                if (value.length == 0) {
                    passwordComplex = 0;
                } else {
                    var modes = 0;
                    for (i = 0; i < value.length; i++) {
                        var c = charMode(value.charCodeAt(i));
                        modes |= c;
                    }
                    passwordComplex = bitTotal(modes);
                }
                $('.password-power span').attr('class', 'empty');
                if (passwordComplex == 0) {
                    return;
                }
                $('.password-power span').each(function (index) {
                    var i = index + 1;
                    $(this).attr('class', 'good');
                    if (i == passwordComplex) {
                        return false;
                    }
                });
			});
			
			$("#glodon_password_confirm").blur(function(){
				var t = $(this), 
				v = $(this).val();
				if(v === ''){
					self.updateMsg('请重复输入密码', t);
				}else if( $("#glodon_password").val() !== $(this).val() ){
		    		self.updateMsg('两次输入的密码不一致', t);
				}else if(v.length >= 6 && v.length <= 16){
					self.onCorrect(t);
				}
			});
			
			$("#glodon_register_container input").keypress(function(e){
			    if(e.which === 13){
			        $("#glodon_register_submit a").trigger("click");
			    }
			});
			
			$("#glodon_register_submit a").click(function(e){
				var flag = true,
				errors = $('#glodon_register_container .error');
				if(errors.length > 0){
					self.updateMsg('请输入正确的信息');
					errors.eq(0).find('input').focus();
					flag = false;
				}
				
				if(flag){
					$("#register_form").submit();
				}
				return false;
			});
			if($("#glodon_reg_error").hasClass("login_error")){
				self.updateMsg('注册失败，请重新输入');
			}
		}
	}
	$(function(){
		register.init();
	});
})(jQuery);
